﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BonjourHotel.Core.Configuration;
using BonjourHotel.Core.Cache;
using BonjourHotel.Core.Context;

namespace BonjourHotel.Core.Localization
{
    public class LocaleManager
    {
        private static readonly string LOCALESTRINGKEY = "BH-LOCALESTRING-{0}-{1}";
        
        public static string DefaultLanguage
        {
            get
            {
                return "en-US";
            }
        }


        public static string GetLocaleString(string key)
        {
            string currentLanguage = BonjourHotelContext.WorkingLanguage;
            string wordKey = key.GetHashCode().ToString();
            string cacheKey = String.Format(LOCALESTRINGKEY, currentLanguage, wordKey);

            if (ConfigurationManager.IsCacheEnabled)
            {
                object value = CacheManager.Get(cacheKey);
                if (value != null)
                    return (string)value;
            }

            LocaleStringResourceFacade facade = new LocaleStringResourceFacade();
            string strValue = facade.GetStringResource(key, currentLanguage);
            if (String.IsNullOrEmpty(strValue)) strValue = key;

            if (ConfigurationManager.IsCacheEnabled && strValue != key)
            {
                CacheManager.Max(cacheKey, strValue);
            }

            return strValue;
        }
    }
}
